/*
 * @Author: baoyiwen 511530203@qq.com
 * @Date: 2025-04-22 15:28:50
 * @LastEditors: baoyiwen 511530203@qq.com
 * @LastEditTime: 2025-05-13 17:51:06
 * @FilePath: \neilao-mobile\src\router\index.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%A
 */
import { createRouter, createWebHashHistory } from 'vue-router'
import axios from 'axios'
import dd from 'gdt-jsapi'
import {
  getToken,
  setToken,
  setUserInfo,
  setDataToken,
  getUserInfo,
  getDataToken,
  clearAllUserInfo,
} from '../utils/userInfo'
// import { getUserInfo, getTokenByCode, getDataToken } from "../api/index";
import { showSuccessToast, showFailToast } from 'vant'
import { patrol } from './patrol'
import { alarm } from './alarm'
const routes = [
  {
    name: 'Login',
    path: '/login',
    component: () => import('../views/login.vue'),
  },
  {
    path: '/',
    redirect: '/workStation',
    component: () => import('../views/login.vue'),
  },
  {
    name: 'home',
    path: '/home',
    component: () => import('../views/home/index.vue'),
    meta: { showTabbar: true },
  },
  {
    name: 'homeDetail',
    path: '/detail',
    component: () => import('../views/home/detail.vue'),
    meta: { showTabbar: false },
  },
  {
    name: 'arrive',
    path: '/arrive',
    component: () => import('../views/home/components/arrive.vue'),
    meta: { showTabbar: false },
  },
  {
    name: 'feedback',
    path: '/feedback',
    component: () => import('../views/home/components/feedback.vue'),
    meta: { showTabbar: false },
  },
  {
    name: 'reinforceCompletedForm',
    path: '/reinforceCompletedForm',
    title: '增援-提交办结',
    component: () =>
      import('../views/home/components/reinforceCompletedForm.vue'),
    meta: { showTabbar: false },
  },
  {
    name: 'detailRe',
    path: '/detailRe',
    component: () => import('../views/home/detailRe.vue'),
    meta: { showTabbar: false },
  },
  {
    name: 'video',
    path: '/video',
    component: () => import('../views/communicate/index.vue'),
  },
  {
    name: 'my',
    path: '/my',
    component: () => import('../views/user/index.vue'),
    meta: { showTabbar: true },
  },
  {
    name: 'inPosition',
    path: '/inPosition',
    component: () => import('../views/home/components/inPosition.vue'),
  },
  {
    name: 'completeReport',
    path: '/completeReport',
    component: () => import('../views/home/components/completeReport.vue'),
  },
  {
    name: 'quickReporting',
    path: '/quickReporting',
    component: () => import('../views/quickReporting/index.vue'),
  },
  {
    name: 'quickReportSecondary',
    path: '/quickReportSecondary',
    component: () =>
      import('../views/quickReporting/components/quickReportSecondary.vue'),
  },
  {
    name: 'Auth Page',
    path: '/auth-page',
    component: () => import('../views/authpage/auth.vue'),
  },
  {
    name: 'workStation',
    path: '/workStation',
    component: () => import('../views/workStation/index.vue'),
    meta: { showTabbar: true },
  },
  {
    name: 'me',
    path: '/me',
    component: () => import('../views/me/index.vue'),
    meta: { showTabbar: true },
  },
  {
    name: 'task',
    path: '/task',
    component: () => import('../views/task/index.vue'),
    meta: { showTabbar: true },
  },
  {
    name: 'responesTaskCreateOrder',
    path: '/responesTaskCreateOrder',
    component: () =>
      import('../views/task/components/responesTaskCreateOrder.vue'),
    // meta: { showTabbar: true },
  },
  {
    name: 'disposeEventCreateOrder',
    path: '/disposeEventCreateOrder',
    component: () =>
      import('../views/task/components/disposeEventCreateOrder.vue'),
    // meta: { showTabbar: true },
  },
  {
    name: 'riskPoint',
    path: '/riskPoint',
    component: () => import('../views/waterloggingRisk/riskPoint.vue'),
    // meta: { showTabbar: true },
  },
  {
    name: 'pointManagement',
    path: '/pointManagement',
    component: () =>
      import('../views/waterloggingRisk/components/pointManagement.vue'),
    // meta: { showTabbar: true },
  },
  {
    name: 'pointDetail',
    path: '/pointDetail',
    component: () =>
      import('../views/waterloggingRisk/components/pointDetail.vue'),
    // meta: { showTabbar: true },
  },
  {
    name: 'disposeEventDetail',
    path: '/disposeEventDetail',
    component: () => import('../views/task/components/disposeEventDetail.vue'),
    // meta: { showTabbar: true },
  },
  {
    name: 'responseTaskDetail',
    path: '/responseTaskDetail',
    component: () =>
      import('../views/task/components/responesTaskDetais/index.vue'),
    // meta: { showTabbar: true },
  },
  {
    name: 'statisticInfo',
    path: '/statisticInfo',
    component: () => import('../views/task/components/statisticInfo.vue'),
    // meta: { showTabbar: true },
  },
  ...patrol,
  ...alarm,
]

const router = createRouter({
  history: createWebHashHistory(((import.meta as any).env as any).BASE_URL),
  routes,
  // 刷新时，滚动条位置还原
  scrollBehavior: () => ({ left: 0, top: 0 }),
} as any)

// const resDataToken = (token) => {
//   getDataToken(token).then((res) => {
//     if (res.data.code === 200) {
//       setDataToken(res.data.message);
//     }
//   });
// };

router.beforeEach((to, from, next) => {
  // console.log('to', to)
  // console.log('from', from)
  // 每次愉快证入口进入时，重置登录信息
  if (to.query.resetLogin && to.query.resetLogin === 'true') {
    console.log('重置登录信息')
    clearAllUserInfo()
  }
  const token = getToken()
  const dataToken = getDataToken()
  const userInfo = getUserInfo()
  if (token && dataToken && userInfo) {
    next()
  } else {
    if (to.path === '/login' || to.path === '/auth-page') {
      next()
    } else {
      dd.getAuthCode({})
        .then((res) => {
          router.push('/auth-page')
        })
        .catch((err) => {
          router.push('/login')
        })
    }
  }
  // getUserInfo(token)
  //   .then((res) => {
  //     if (res.data.code === 200) {
  //       if (to.path !== "/login") {
  //         next();
  //       } else {
  //         router.push("/home");
  //       }
  //     } else {
  //       dd.getAuthCode({})
  //         .then((res) => {
  //           /// const data = JSON.parse(res.data.data);
  //           console.log(res, "code");
  //           const authCode = res.code;
  //           showSuccessToast("获取渝快政登陆验证码成功!");
  //           setTimeout(() => {
  //             getTokenByCode(authCode)
  //               .then((tokenRes) => {
  //                 if (tokenRes.data.code == 200) {
  //                   showSuccessToast("获取登陆验证成功!");
  //                   const data = JSON.parse(tokenRes.data.data);
  //                   console.log(data, "token");
  //                   let token = data.token;
  //                   setToken(token);
  //                   getUserInfo(token).then((res) => {
  //                     if (res.data.code === 200) {
  //                       setUserInfo(res.data.data);
  //                       router.push("/home");
  //                     }
  //                   });
  //                   resDataToken(token);
  //                 } else {
  //                   setToken(null);
  //                   // ElMessage.error(
  //                   //   "尊敬的用户，根据应用权限设置，您没有本应用访问权限本应用责任部门为重庆市住房和城乡建设委员会若您需使用本应用，请拨打联系电话: 023-63670927，感谢您的理解支持!"
  //                   // );
  //                 }
  //               })
  //               .catch((res) => {
  //                 showFailToast("获取渝快政登陆验证失败!");
  //                 router.push("/login");
  //               });
  //           }, 0);
  //         })
  //         .catch((err) => {
  //           showFailToast("获取渝快政登陆验证码失败!");
  //           router.push("/login");
  //         });
  //     }
  //   })
  //   .catch(() => {
  //     dd.getAuthCode({})
  //       .then((res) => {
  //         /// const data = JSON.parse(res.data.data);
  //         console.log(res, "code");
  //         const authCode = res.code;
  //         showSuccessToast("获取渝快政登陆验证码成功!");
  //         setTimeout(() => {
  //           getTokenByCode(authCode)
  //             .then((tokenRes) => {
  //               if (tokenRes.data.code == 200) {
  //                 showSuccessToast("获取登陆验证成功!");
  //                 const data = JSON.parse(tokenRes.data.data);
  //                 let token = data.token;
  //                 console.log(data, "token");
  //                 setToken(token);
  //                 // store.dispatch("update_token", token);
  //                 getUserInfo(token).then((res) => {
  //                   if (res.data.code === 200) {
  //                     setUserInfo(res.data.data);
  //                     router.push("/home");
  //                   }
  //                 });
  //                 resDataToken(token);
  //               } else {
  //                 showFailToast("获取渝快政登陆验证码失败!");
  //                 setToken(null);
  //                 router.push("/login");
  //               }
  //             })
  //             .catch((res) => {
  //               showFailToast("获取渝快政登陆验证失败!");
  //               router.push("/login");
  //             });
  //         }, 0);
  //       })
  //       .catch((err) => {
  //         showFailToast("获取渝快政登陆验证码失败!");
  //         if (to.path === "/login") {
  //           next();
  //         } else {
  //           router.push("/login");
  //         }
  //       });
  //     // if (to.path === "/login") {
  //     //   next();
  //     // } else {
  //     //   router.push("/login");
  //     // }
  //   });
})

export default router
